//TOP K

class Solution {
public:
    int random(vector<int>& nums, int l, int r)
    {
        return nums[rand() % (r - l + 1) + l];
    }
    int qsort(vector<int>& nums, int l, int r, int k)
    {
        if(l == r) return nums[l];
        int key = random(nums, l, r);
        int left = l - 1, right = r + 1;
        //[l, left][left + 1, right - 1][right, r]
        int i = l;//i为已扫描区域的下标
        while(i < right)
        {
            if(nums[i] < key)  swap(nums[++left],nums[i++]);
            else if(nums[i] == key) i++;
            else swap(nums[i],nums[--right]);
        }
        if(r - right + 1 >= k) return qsort(nums, right, r, k);
        else if(r - left >= k) return key;
        else return qsort(nums, l, left, k - (r - left));
    }
    int findKthLargest(vector<int>& nums, int k) {
        int n = nums.size();
        srand(time(NULL));
        return qsort(nums, 0, n-1, k);
    }
};
